#!/usr/bin/env ruby
#encoding: utf-8

require 'action_mailer'

module Mail_VTB
  
  # TODO: Separate these mail configuration into different files?
  mail_type = $hash_options[:mail]
  case mail_type
  when '163'  
    sender = 'valuable_to_buy@163.com'
    
    ActionMailer::Base.smtp_settings = {
      address:              'smtp.163.com',
      port:                 25,
      domain:               '163.com',
      user_name:            sender,
      password:             'Qqqqq12345',
      authentication:       'login',
      enable_starttls_auto: true
    }
  when 'mstr'
    sender = 'jugu@microstrategy.com'
    
    ActionMailer::Base.smtp_settings = {
      address:              'CORP-EXCL1-WAS'
    }
  when 'sendmail'
    sender = 'gjtiger@gmail.com'

    ActionMailer::Base.delivery_method = :sendmail
    ActionMailer::Base.raise_delivery_errors = true
    ActionMailer::Base.perform_deliveries = true
  when 'gmail', nil
    sender = 'nisprival@gmail.com'
    
    ActionMailer::Base.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 25,
      domain:               'gmail.com',
      user_name:            sender,
      password:             '19851127',
      authentication:       'login',
      enable_starttls_auto: true
    }
    
    puts 'No mail server specified, use gmail by default.' if mail_type.nil?
  else
    puts 'Error since we should not reach here!'
  end
  
  # TODO: use module accessor to access this variable?
  SENDER = sender
  
  # TODO: move these settings into a separate configuration file, then read from it
  # TODO: use something like mail_queue to send mail in an asynchronized way to reduce workload
  # eg. write info into database, then use another application to send mail

  # use ActionMailer without Rails
  ActionMailer::Base.view_paths = File.dirname(__FILE__)
  
  class NotificationMail < ActionMailer::Base
    
    default :from => SENDER
    
    def notification toMail, items
      
      @items = items
      
      mail(to: toMail, subject: "Valuable To Buy") do |format|
        format.text
        format.html
      end
      
    end
    
  end
  
  if __FILE__ == $0
    
    $hash_options = {}
    $hash_options[:mail] = '163'
    
    # toMail = "jugu@microstrategy.com"
    toMail = SENDER
    # toMail = "qiuyin@gmail.com"
    
    data = []
    
    require 'backend/crawler/html_parser'
    rtn = Crawler::HtmlParser.crawl
    data << rtn
    # puts rtn
    
    start = Time.now
    mail = NotificationMail.notification toMail, data
    mail.deliver
    elapsed = Time.now - start
    puts "Used #{elapsed} time to send first mail"
    # puts mail
#     
    start = Time.now
    mail = NotificationMail.notification toMail, data
    mail.deliver
    elapsed = Time.now - start
    puts "Used #{elapsed} time to send second mail"
    # puts mail
    
    # Net::SMTP.start('CORP-EXCL1-WAS') do |smtp|
      # smtp.sendmail('hello mail', 'bwang@microstrategy.com', 'jugu@microstrategy.com')
      # puts 'sent'
    # end
    
  end
  
end
